#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define se second
#define fi first
#define pii pair<int,int>
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int MAXN = 8e4+10;

int n,m,val[MAXN],f[MAXN],nodenum,bel[MAXN],dfn[MAXN],tot,low[MAXN],stk[MAXN],top,s,ans;
struct edge{int v,w;db c;};
vector<edge>G[MAXN];
vector<pii> T[MAXN];
bool vis[MAXN];

void tarjan(int x){
	low[x] = dfn[x] = ++tot; stk[++top] = x;vis[x] = 1;
	for(auto d : G[x]){
		int y = d.v;
		if(!dfn[y]) tarjan(y),low[x] = min(low[x],low[y]);
		else if(vis[y]) low[x] = min(low[x],dfn[y]);
	}
	if(low[x] == dfn[x]){
		++nodenum;
		do{
			bel[stk[top]] = nodenum; vis[stk[top]] = 0;
		}while(stk[top--] != x);
	}
}

int dfs(int x){
	int tot = 0;
	for(auto d:T[x]){
		int y = d.fi,w = d.se;
		tot = max(tot,dfs(y)+w);
	}return tot + val[x];
}

int main (){
	n = in,m = in;
	for(int i = 1;i <= m;i++){
		int x = in,y = in,w = in;db c;scanf("%lf",&c);
		G[x].push_back((edge){y,w,c});
	}s = in; for(int i = 1;i <= n;i++) if(!dfn[i]) tarjan(i);
	for(int i = 1;i <= n;i++)
		for(auto d:G[i]){
			int y = d.v,w = d.w;
			if(bel[i] == bel[y]) while(w) val[bel[i]] += w,w *= d.c;
			else T[bel[i]].pb(pii(bel[y],w));
		}
	int ans = dfs(bel[s]); printf("%d\n",ans);
	return 0;
}

